package com.tianma315.wx.config;


import com.tianma315.core.common.utils.PasswordUtils;

import com.tianma315.core.sys.service.UserService;
import com.tianma315.core.sys.service.impl.UserServiceImpl;
import com.tianma315.wx.service.UserServices;
import com.tianma315.wx.service.impl.SocialUserDetailsServiceImpl;
//import com.tianma315.wx.service.impl.UserDetailsServiceImpl;

import com.tianma315.wx.service.impl.UserServicesImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.social.security.SpringSocialConfigurer;


//                   _ooOoo_
//                  o8888888o
//                  88" . "88
//                  (| -_- |)
//                  O\  =  /O
//               ____/`---'\____
//             .'  \\|     |//  `.
//            /  \\|||  :  |||//  \
//           /  _||||| -:- |||||-  \
//           |   | \\\  -  /// |   |
//           | \_|  ''\---/''  |   |
//           \  .-\__  `-`  ___/-. /
//         ___`. .'  /--.--\  `. . __
//      ."" '<  `.___\_<|>_/___.'  >'"".
//     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//     \  \ `-.   \_ __\ /__ _/   .-` /  /
//======`-.____`-.___\_____/___.-`____.-'======
//                   `=---='
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//         佛祖保佑       永无BUG

/**
 * web安全配置
 * <p>
 * Created by zcm on 2018/5/14.
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)//开启security注解
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {

    private String loginUrl = "/wx/login";

    @Autowired
    private UserServices userServide;

    @Autowired
    private SpringSocialConfigurer springSocialConfigurer;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        super.configure(auth);
//        auth.userDetailsService(userDetailsService())
//                .passwordEncoder(passwordEncoder());
        auth.authenticationProvider(authenticationProvider());
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        super.configure(web);
        web.ignoring()
                .mvcMatchers("/**/*.*");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/login","/open/**","/auth/**","/wx/socialRegister","/wx/bindUser")
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin()
                .loginPage(loginUrl)
                .loginProcessingUrl("/login")
                .defaultSuccessUrl("/wx/main")
                .failureUrl(loginUrl + "?error=true")
                .permitAll()
                .and()
                .apply(springSocialConfigurer)
                .and()
                .logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl(loginUrl + "?logout=true")
                .permitAll();
        //
        http.csrf().disable();
        http.httpBasic().disable();
        http.headers().frameOptions().disable();
    }

    @Bean
    PasswordEncoder passwordEncoder() {
        return PasswordUtils.getPasswordEncoder();
    }



    @Bean
    public UserDetailsService userDetailsService() {
        return new UserServicesImpl();
    }


    @Bean
    public AuthenticationProvider authenticationProvider() {
        return new WxDaoAuthenticationProvider() {{
            setUserDetailsService(userDetailsService());
            setPasswordEncoder(passwordEncoder());
//            setSaltSource((user) -> {
//                return user.getUsername();
//            });
        }};
    }

}
